package com.sec.android.app.myfiles.module.local.recent;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.module.download.DownloadFileRecord;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import java.io.File;

public class RecentFileRecord extends DownloadFileRecord {
    private long mLastAccessedTime;
    public RecentFileRecord(String fullPath) {
        super(fullPath);
        mStorageType = StorageType.Recent;
        if (!TextUtils.isEmpty(fullPath)) {
            mFileType = MediaFile.getFileType(fullPath);
            setFileInfo(fullPath);
        }
    }

    public RecentFileRecord(String fullPath, StorageType type) {
        super(fullPath);
        mStorageType = type;
    }

    public RecentFileRecord(int downloadId, String path, String name, long size, long date, int fileType, String source,
                            String description, int status, int download, long lastAccessedTime) {
        super(-1, downloadId, path, name, size, date, fileType, source, description, status, download);
        setFileInfo(getFullPath(path, name));
        mStorageType = StorageType.Recent;
        mLastAccessedTime = lastAccessedTime;
    }

    private void setFileInfo(String fullPath) {
        File file = SemFwWrapper.file(fullPath);
        if (file.exists()) {
            mDate = file.lastModified();
            mSize = file.length();
        }
    }

    @Override
    protected int _getId() {
        return -1;
    }

    @Override
    protected Uri _getUri(Context context) {
        Uri ret = FileUtils.getUriByPath(context, getFullPath());

        if (ret == null) {
            ret = FileUtils.getContentUri(context, getFullPath());
        }

        return ret;

    }

    @Override
    public boolean exists(Context context) {
        boolean ret;
        if (isRoot()) {
            ret = true;
        } else {
            File file = SemFwWrapper.file(getFullPath());
            ret = file.exists();
        }
        return ret;
    }

    @Override
    public String getDisplayName(Context context) {
        return context.getString(R.string.subtitle_recent);
    }

    public long getLastAccessedTime() {
        return mLastAccessedTime;
    }
}
